﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Configuration;
using System.Data.SqlClient;

namespace ScoreManage
{
    public partial class StudentManage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {   
            if (Session["UserType"].ToString() == "分管教学领导")
            {
                btnInsert.Visible = false;
                GridView1.Visible = false;
                GridView2.Visible = true;
            }
            else
            {
                btnInsert.Visible = true;
                GridView1.Visible = true;
                GridView2.Visible = false;
            }
            if (Session["UserType"].ToString() == "系统管理员")
            {
                btnDepartment.Visible = true;
            }
            else
            {
                btnDepartment.Visible = false;
            }
            BindGrid();
        }

        protected void BindGrid()
        {
            string strConn = ConfigurationManager.ConnectionStrings["studbConnectionString"].ConnectionString;
            SqlConnection conn = new SqlConnection(strConn);
            string strSql;
            if (Session["UserType"].ToString() == "系统管理员")
            {
                strSql = "SELECT * FROM student";
            }
            else
            {
                strSql = string.Format("SELECT * FROM student WHERE department='{0}'", Session["Department"].ToString()) ;
            }
            SqlCommand myCommand = new SqlCommand(strSql, conn);
            SqlDataAdapter da = new SqlDataAdapter(myCommand);
            DataSet ds = new DataSet();
            da.Fill(ds, "student");
            GridView1.DataSource = ds.Tables["student"];
            GridView1.DataBind();
            GridView2.DataSource = ds.Tables["student"];
            GridView2.DataBind();
        }

        protected void lbtnEdit_Click(object sender, EventArgs e)
        {
            string Sno = ((LinkButton)sender).CommandArgument.ToString();
            Response.Redirect(string.Format("~/StudentEdit.aspx?item=2&sno={0}", Sno));
        }

        protected void lbtnDelete_Click(object sender, EventArgs e)
        {
            string Sno = ((LinkButton)sender).CommandArgument.ToString();
            string strConn = ConfigurationManager.ConnectionStrings["studbConnectionString"].ConnectionString;
            SqlConnection conn = new SqlConnection(strConn);
            conn.Open();
            string strSql = string.Format("DELETE student WHERE sno = '{0}'", Sno);
            SqlCommand myCommand = new SqlCommand(strSql, conn);
            myCommand.ExecuteNonQuery();
            BindGrid();
        }

        protected void btnInsert_Click(object sender, EventArgs e)
        {
            Response.Redirect("StudentInsert.aspx?item=2");
        }

        protected void btnSno_Click(object sender, EventArgs e)
        {
            string Sno = txtQuery.Text.Trim().Replace("'", "''");
            string strConn = ConfigurationManager.ConnectionStrings["studbConnectionString"].ConnectionString;
            SqlConnection conn = new SqlConnection(strConn);
            string strSql;
            if (Session["UserType"].ToString() == "系统管理员")
            {
                strSql = string.Format("SELECT * FROM student WHERE sno LIKE '%{0}%'", Sno);
            }
            else
            {
                strSql = string.Format("SELECT * FROM student WHERE sno LIKE '%{0}%' AND department='{1}'", Sno, Session["Department"].ToString());
            }
            SqlCommand myCommand = new SqlCommand(strSql, conn);
            SqlDataAdapter da = new SqlDataAdapter(myCommand);
            DataSet ds = new DataSet();
            da.Fill(ds, "student");
            GridView1.DataSource = ds.Tables["student"];
            GridView1.DataBind();
            GridView2.DataSource = ds.Tables["student"];
            GridView2.DataBind();
        }

        protected void btnSname_Click(object sender, EventArgs e)
        {
            string Sname = txtQuery.Text.Trim().Replace("'", "''");
            string strConn = ConfigurationManager.ConnectionStrings["studbConnectionString"].ConnectionString;
            SqlConnection conn = new SqlConnection(strConn);
            string strSql;
            if (Session["UserType"].ToString() == "系统管理员")
            {
                strSql = string.Format("SELECT * FROM student WHERE sname LIKE '%{0}%'", Sname);
            }
            else
            {
                strSql = string.Format("SELECT * FROM student WHERE sname LIKE '%{0}%' AND department='{1}'", Sname, Session["Department"].ToString());
            }
            SqlCommand myCommand = new SqlCommand(strSql, conn);
            SqlDataAdapter da = new SqlDataAdapter(myCommand);
            DataSet ds = new DataSet();
            da.Fill(ds, "student");
            GridView1.DataSource = ds.Tables["student"];
            GridView1.DataBind();
            GridView2.DataSource = ds.Tables["student"];
            GridView2.DataBind();
        }

        protected void btnDepartment_Click(object sender, EventArgs e)
        {
            string Department = txtQuery.Text.Trim().Replace("'", "''");
            string strConn = ConfigurationManager.ConnectionStrings["studbConnectionString"].ConnectionString;
            SqlConnection conn = new SqlConnection(strConn);
            string strSql = string.Format("SELECT * FROM student WHERE department LIKE '%{0}%'", Department);
            SqlCommand myCommand = new SqlCommand(strSql, conn);
            SqlDataAdapter da = new SqlDataAdapter(myCommand);
            DataSet ds = new DataSet();
            da.Fill(ds, "student");
            GridView1.DataSource = ds.Tables["student"];
            GridView1.DataBind();
            GridView2.DataSource = ds.Tables["student"];
            GridView2.DataBind();
        }
    }
}